<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/3
 * Time: 下午6:14
 */

namespace App\HttpController;
use App\Model\Server\ConnectingResult;
use AutoMsg\MsgBaseRev;
use AutoMsg\RoleLists;
use EasySwoole\Core\Http\AbstractInterface\Controller;
use EasySwoole\Core\Http\Message\Status;
use EasySwoole\Core\Swoole\ServerManager;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\MsgSendBase;
use GPBMetadata\Google\Protobuf\Protomsg;
use Kjcx\helloworld;
use think\Db;
use EasySwoole\Core\Component\Di;

class Index extends Controller
{
    //测试路径 /index.html
    function index()
    {
        $ConnectingResult = new \AutoMsg\ConnectingResult();
        $RoleLists = new RoleLists();
        $RoleLists->setName('kjcx');
        $RoleLists->setSex(1);
        $arr[] = $RoleLists;
        $ConnectingResult->setRoleLists($arr);
        $ss =  $ConnectingResult->serializeToString();

        $MsgSendBase =  new \AutoMsg\MsgSendBase();
        $MsgSendBase->setMsgID(1057);
        $MsgSendBase->setData($ss);
//        $MsgSendBase->setResult(1);
        $str = $MsgSendBase->serializeToString();
        $this->response()->write($str);

        // $res = Db::table('user')
        //     ->data(['name'=>'thinkphp','email'=>'thinkphp@qq.com','str'=>rand()])
        //     ->insert();
        // $arr = Db::table('user')->select();
//        $this->response()->write($ConnectingResult->getRoleLists();


        //测试
//        $pt = new MsgSendBase();
////        解析数据
//        $str = hex2bin('08f8071a460a440a0ce4b887e4bf9fe699afe6be8412086e70635f3130303130b1c681d5054a20663738353762646236663566343732363866373538346262386338333339393550017200');
//        $pt->mergeFromString($str);
//        echo $pt->getMsgID();
//        $this->response()->write('123hello world');
        //测试
        $pt = new MsgSendBase();
//        解析数据
        $str = hex2bin('08f8071a460a440a0ce4b887e4bf9fe699afe6be8412086e70635f3130303130b1c681d5054a20663738353762646236663566343732363866373538346262386338333339393550017200');
        $pt->mergeFromString($str);
        echo $pt->getMsgID();
        $this->response()->write('123hello world');
    }
    //测试路径 /test/index.html
    function test()
    {
        $ip = ServerManager::getInstance()->getServer()->connection_info($this->request()->getSwooleRequest()->fd);
        var_dump($ip);
        $ip2 = $this->request()->getHeaders();
        var_dump($ip2);
        $this->response()->write('index controller test');
    }

    /*
     * protected 方法对外不可见
     *  测试路径 /hide/index.html
     */
    protected function hide()
    {
        var_dump('this is hide method');
    }

    protected function actionNotFound($action): void
    {
        $this->response()->withStatus(Status::CODE_NOT_FOUND);
        $this->response()->write("{$action} is not exist");
    }

    function a()
    {
        $this->response()->write('index controller router');
    }

    function a2()
    {
        $this->response()->write('index controller router2');
    }

    function test2(){
        $this->response()->write('this is controller test2 and your id is '.$this->request()->getRequestParam('id'));
    }
}